
#define  _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"
#include "menu.h"


#define MAX_LEN 100
/****************************************************************************************/
/*结构区*/
typedef struct Student {
	int stu_id; //学号  //2023001
	char  stu_name[100]; //姓名
	int age; //年龄
	char gender[10]; //性别
	char stu_major[10]; //专业
	int class_id; //班级
}Stu;
/*
typedef struct Manager {
Stu students[MAX_LEN];
int stu_size; //表示当前的记录数
}StuMan;
*/


/*数据区*/
Stu students[MAX_LEN];
int arr_len = 0;
/****************************************************************************************/
/*功能区*/

//添加学生
void addStu() {
	printf("-----------------------------------------\n");
	printf("|\t\t添加学生信息\t\t|\n");
	printf("-----------------------------------------\n");
	/*printf("学号（int）:	");
	scanf("%d", &students[arr_len].stu_id);
	printf("姓名（char*）:	");
	scanf("%s", students[arr_len].stu_name);
	printf("年龄（int）:	");
	scanf("%d", &students[arr_len].age);
	printf("性别（char*）:	");
	scanf("%s", students[arr_len].gender);
	printf("专业（char*）:	");
	scanf("%s", students[arr_len].stu_major);
	printf("班级（char*）:	");
	scanf_s("%d", &students[arr_len].class_id);*/
	
	printf("学号\t\t姓名\t\t年龄\t\t性别\t\t专业\t\t班级\n");
	scanf("%d%s%d%s%s%d", &students[arr_len].stu_id, students[arr_len].stu_name, &students[arr_len].age, students[arr_len].gender, students[arr_len].stu_major, &students[arr_len].class_id);
	printf("添加完毕！");
	arr_len++;

}
//删除学生
void deleteStu(int size) {
	for (int i = size; i < arr_len; i++) {
		students[i] = students[i + 1];
	}
	arr_len--;
	printf("删除完毕！");
}
//修改学生信息
void updataStu(int size) {
	printf("-----------------------------------------\n");
	printf("|\t\t修改学生信息\t\t|\n");
	printf("-----------------------------------------\n");
/*int num;
	char name[20];
	int age;
	char gender[10];
	char major[20];
	int class_id;
*/	
	printf("学号（int）:	");
	scanf("%d", &students[size].stu_id);
	printf("姓名（char*）:	");
	scanf("%s", students[size].stu_name);
	printf("年龄（int）:	");
	scanf("%d", &students[size].age);
	printf("性别（char*）:	");
	scanf("%s", students[size].gender);
	printf("专业（char*）:	");
	scanf("%s", students[size].stu_major);
	printf("班级（char*）:	");
	scanf_s("%d", &students[size].class_id);
	printf("修改完毕！");
}
//查找学生信息
int findStu() {
	printf("-----------------------------------------\n");
	printf("|\t\t学生信息查询表\t\t|\n");
	printf("-----------------------------------------\n");
	int stu_id;
	stus:
	printf("请输入你要查找的学生id:	");
	scanf("%d", &stu_id);
	if (arr_len != 0) {
		for (int i = 0; i < arr_len; i++) {
			if (students[i].stu_id == stu_id) {
				return i;
			}
			else {
				continue;
			}
		}
		return -1;
	}
	else {
		printf("该表无数据！");
		return -1;
	}
	
}
//输出查找学生列表
void outPutStu(int j) {
	printf("-----------------------------------------\n");
	printf("|\t\t学生信息列表为\t\t|\n");
	printf("-----------------------------------------\n");
	printf("\n\n");
	printf("学号\t\t姓名\t\t年龄\t\t性别\t\t专业\t\t班级\n");
		printf("%-8d\t", students[j].stu_id);
		printf("%-8s\t", students[j].stu_name);
		printf("%-8d\t", students[j].age);
		printf("%-8s\t", students[j].gender);
		printf("%-8s\t", students[j].stu_major);
		printf("%-8d\n", students[j].class_id);
		system("pause");
	}

//列出学生列表
void printStuList() {
	int i = arr_len;

	if (i == 0) {
		i = 1;
	} 
	printf("-----------------------------------------\n");
	printf("|\t\t学生信息列表为\t\t|\n");
	printf("-----------------------------------------\n");
	printf("\n\n");
	printf("学号\t\t姓名\t\t年龄\t\t性别\t\t专业\t\t班级\n");
	for (int j = 0; j < i; j++) {
		printf("%-8d\t", students[j].stu_id);
		printf("%-8s\t", students[j].stu_name);
		printf("%-8d\t", students[j].age);
		printf("%-8s\t", students[j].gender);
		printf("%-8s\t", students[j].stu_major);
		printf("%-8d\n", students[j].class_id);
	}
	/*
	printf("学号\t\t年龄\t\t班级\n");
	if (i == 0) {
	i = 1;
	}
	for (int j=0 ; j< i; j++) {
	printf("%d\t\t", students[j].stu_id);
	printf("%d\t\t", students[j].age);
	printf("%d\n", students[j].class_id);
	}
	*/


}

//学生列表排序
void orderStu(int selec) {
	if (selec == 1) {
		int i, j, t;
		for (i = 0; i < arr_len; i++) {
			for (j = 0; j < arr_len - i; j++) {
				if (students[j].stu_id < students[j + 1].stu_id) {
					t = students[j].stu_id;
					students[j].stu_id = students[j + 1].stu_id;
					students[j + 1].stu_id = t;
				}
			}
		}
	}
	else {
		int i, j, t;
		for (i = 0; i < arr_len; i++) {
			for (j = 0; j < arr_len - i; j++) {
				if (students[j].stu_id > students[j + 1].stu_id) {
					t = students[j].stu_id;
					students[j].stu_id = students[j + 1].stu_id;
					students[j + 1].stu_id = t;
				}
			}
		}
	}
	printf("排序完毕！");
	system("pause");
}

//保存学生信息数据
void SaveStu() {

}
//读取学生信息数据
void ReadStu() {

}
//判断用户输入的是升序还是降序（按照学号id进行排序）
int isDiscending() {
	//默认升序
	int Sign = 0;
	printf("请输入[0:Asc,1:Disc]: ");
	int user_select;
	scanf("%d", &user_select);
	if (user_select == 1) {
		Sign = 1;
	}
	return Sign;
}





/****************************************************************************************/
int main() {
	//初始化数据

	int User_select; //用户的选择
	int stu_size;
	int select;

	while (1) {
		menu(); //显示菜单
		printf("请输入[ 0 - 8 ] ：");
		scanf_s("%d", &User_select);
		switch (User_select)
		{
		case 1:
			//添加学生
			system("cls");
			addStu();
			break;
		case 2:
			stu_size = findStu();
			deleteStu(stu_size);
			//删除学生
			break;
		case 3:
			//修改学生信息
			stu_size = findStu();
			updataStu(stu_size);
			system("pause");
			break;
		case 4:
			//查找学生
			stu_size = findStu();
			outPutStu(stu_size);
			break;
		case 5:
			//查看列表学生
			system("cls");
			printStuList();
			system("pause");
			break;
		case 6:
			//学生排序
			select = isDiscending();
			orderStu(select);
			break;
		case 7:
			//写入学生信息进数据库
			break;
		case 8:
			//读取数据库中的学生信息
			break;
		case 0:
			exit(0);
			break;
		}

	}


}